//
//  LLBottomTitleCollectionViewCell.m
//  lrlzapp
//
//  Created by dl on 16/2/29.
//  Copyright © 2016年 lrlz. All rights reserved.
//

#import "LLBottomTitleCollectionViewCell.h"
#import "LLBottomTitleViewModel.h"

@interface LLBottomTitleCollectionViewCell ()
@property(nonatomic, strong) LLUIImageView *imageView;
@property(nonatomic, strong) UILabel *titleLabel;

@property(nonatomic, readwrite) LLBottomTitleViewModel *viewModel;

@end

@implementation LLBottomTitleCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.imageView = [LLUIImageView new];
        self.imageView.layer.cornerRadius = SUIT_H(24);
        [self addSubview:self.imageView];

        self.titleLabel = [UILabel new];
        self.titleLabel.font = SUIT_FONT(12);
        self.titleLabel.textColor = UI_COLOR_FONT_LIGHT_GREY;
        self.titleLabel.textAlignment = NSTextAlignmentCenter;
        [self addSubview:self.titleLabel];
    }
    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];
    self.imageView.frame = CGRectMake((self.ll_width - SUIT_H(48)) / 2.f,
            SUIT_H(10),
            SUIT_H(48),
            SUIT_H(48));
    self.titleLabel.frame = CGRectMake(_imageView.ll_left,
            _imageView.ll_bottom + SUIT_H(8),
            SUIT_H(48), SUIT_H(15));
}

- (void)bindViewModel:(id)viewModel {
    self.viewModel = viewModel;

    RAC(self.titleLabel, text) = [RACObserve(self.viewModel, title)
            takeUntil:self.rac_prepareForReuseSignal];
    [[RACObserve(self.viewModel, imageUrl)
            distinctUntilChanged]
            subscribeNext:^(NSString *imageUrl) {
                [self.imageView setImageUrlStr:imageUrl];
            }];
}

@end
